<?php
/**
 * 标准动作模型
 * @author jianhui
 *
 */
class actionModel extends MY_Model{
    function actionModel(){
        parent::MY_Model();
        $this->table = 'Action';
        $this->load->library('Cache');
    }
    /**
     * 包装数据
     * @param array $list
     * @return array
     */
    function packing($list = array()){
        $this->CI->load->model('timeUnitModel');
        $timeUnitList = $this->CI->timeUnitModel->getCacheData('timeUnitList');
        $status = array(1=>'开启',0=>'关闭');
        $type = array(1=>'单店活动',2=>'专项基金',3 =>'城市基金');
        foreach ($list as $k => $v){
            $list[$k]['timeUnitStr'] = isset($timeUnitList[$v['year']][$v['timeUnit']]) ? $timeUnitList[$v['year']][$v['timeUnit']] : '';
            $list[$k]['typeStr'] = isset($type[$v['type']]) ? $type[$v['type']] : '';
            $list[$k]['statusStr'] = isset($status[$v['status']]) ? $status[$v['status']] : '';
        }
        return $list;   
    }
    /**
     * 获取激活的标准动作列表
     * @param array $timeUnit
     * @param number $type
     * @param string $year
     */
    function getActiveList($type = 1, $year = '' , $timeUnit){
        $year || $year = date('Y');
        $actionActiveList = $this->getCacheData('actionActiveList');
        if( isset($actionActiveList[$type][$year][$timeUnit]) ){
            return $actionActiveList[$type][$year][$timeUnit];
        }else{
            return array();
        }
    }
    /**
     * 生成标准动作缓存数据
     */
    function createActionCache(){
        $actionList = array();
        $actionActiveList = array();
        $findList = $this->getList(array('id >'=>0), null, null, '', 'orderNum asc,id asc');
        foreach ($findList as $k => $v){
            //所有标准动作列表
            $actionList[$v['id']] = $v;
            //激活的标准动作列表
            if ($v['status']){
                $actionActiveList[$v['type']][$v['year']][$v['timeUnit']][$v['id']] = $v['name'];
            }
        }
        $this->cache->set('actionList', $actionList);//缓存所有标准动作数据
        $this->cache->set('actionActiveList', $actionActiveList);//缓存激活标准动作数据
    }
    /**
     * 获取标准动作缓存数据
     * @param string $type
     *  actionList：所有标准动作数据
     *  actionActiveList：所有激活的标准动作数据
     * @return mix
     */
    function getCacheData($type){
        $this->cache->expire = 86400;//设置过期时间为一天
        $data = $this->cache->get($type);
        if (!$data){
            //创建缓存
            $this->createActionCache();
            $data = $this->cache->get($type);
        }
        return $data;
    }
}